package com.yang.ddd.interfaces.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.ddd.shared.constant.SpaceUserPermissionConstant;
import com.yang.ddd.shared.auth.annotation.SaSpaceCheckPermission;
import com.yang.ddd.application.service.PictureApplicationService;
import com.yang.ddd.domain.picture.entity.Picture;
import com.yang.ddd.domain.picture.entity.PictureTagCategory;
import com.yang.ddd.domain.user.constant.UserConstant;
import com.yang.ddd.infrastructure.annotation.AuthCheck;
import com.yang.ddd.infrastructure.api.aliyunai.AliYunAiApi;
import com.yang.ddd.infrastructure.api.aliyunai.model.CreateOutPaintingTaskResponse;
import com.yang.ddd.infrastructure.api.aliyunai.model.GetOutPaintingTaskResponse;
import com.yang.ddd.infrastructure.api.imagesearch.model.ImageSearchResult;
import com.yang.ddd.infrastructure.common.BaseResponse;
import com.yang.ddd.infrastructure.common.DeleteRequest;
import com.yang.ddd.infrastructure.common.ErrorCode;
import com.yang.ddd.infrastructure.common.ResultUtils;
import com.yang.ddd.infrastructure.exception.ThrowUtils;
import com.yang.ddd.interfaces.dto.picture.*;
import com.yang.ddd.interfaces.vo.picture.PictureVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * 图片接口
 */
@RestController()
@RequestMapping("/picture")
@Slf4j
public class PictureController {

    @Resource
    private PictureApplicationService pictureApplicationService;

    @Resource
    private AliYunAiApi aliYunAiApi;

    /**
     * 删除图片
     */
    @PostMapping("/delete")
    @ApiOperation("删除图片")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_DELETE)
    public BaseResponse<Boolean> deletePicture(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        pictureApplicationService.deletePicture(deleteRequest, request);
        return ResultUtils.success(true);
    }

    /**
     * 更新图片（仅管理员可用）
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation("更新图片（仅管理员可用）")
    public BaseResponse<Boolean> updatePicture(@RequestBody PictureUpdateRequest pictureUpdateRequest,
                                               HttpServletRequest request) {
        pictureApplicationService.updatePicture(pictureUpdateRequest, request);
        return ResultUtils.success(true);
    }

    /**
     * 根据 id 获取图片（仅管理员可用）
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation("根据 id 获取图片（仅管理员可用）")
    public BaseResponse<Picture> getPictureById(long id) {
        return ResultUtils.success(pictureApplicationService.getPictureById(id));
    }

    /**
     * 根据 id 获取图片（封装类）
     */
    @GetMapping("/get/vo")
    @ApiOperation("根据 id 获取图片（封装类）")
    public BaseResponse<PictureVO> getPictureVOById(long id, HttpServletRequest request) {
        return ResultUtils.success(pictureApplicationService.getPictureVOById(id, request));
    }

    /**
     * 分页获取图片列表（仅管理员可用）
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation("分页获取图片列表（仅管理员可用）")
    public BaseResponse<Page<Picture>> listPictureByPage(@RequestBody PictureQueryRequest pictureQueryRequest) {
        return ResultUtils.success(pictureApplicationService.listPictureByPage(pictureQueryRequest));
    }

    /**
     * 分页获取图片列表（封装类-主页）
     */
    @PostMapping("/list/page/vo")
    @ApiOperation("分页获取图片列表（封装类）")
    public BaseResponse<Page<PictureVO>> listPictureVOByPage(@RequestBody PictureQueryRequest pictureQueryRequest,
                                                             HttpServletRequest request) {
        return ResultUtils.success(pictureApplicationService.listPictureVOByPage(pictureQueryRequest, request));
    }

    /**
     * 编辑图片（给用户使用）
     */
    @PostMapping("/edit")
    @ApiOperation("编辑图片（给用户使用）")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_EDIT)
    public BaseResponse<Boolean> editPicture(@RequestBody PictureEditRequest pictureEditRequest, HttpServletRequest request) {
        pictureApplicationService.editPicture(pictureEditRequest,request);
        return ResultUtils.success(true);
    }

    /**
     * 批量编辑图片
     *
     * @param pictureEditByBatchRequest
     * @param request
     * @return
     */
    @PostMapping("/edit/batch")
    @ApiOperation("批量编辑图片")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_EDIT)
    public BaseResponse<Boolean> editPictureByBatch(@RequestBody PictureEditByBatchRequest pictureEditByBatchRequest, HttpServletRequest request) {
        pictureApplicationService.editPictureByBatch(pictureEditByBatchRequest, request);
        return ResultUtils.success(true);
    }

    /**
     * 以图搜图
     */
    @PostMapping("/search/picture")
    @ApiOperation("以图搜图")
    public BaseResponse<List<ImageSearchResult>> searchPictureByPicture(@RequestBody SearchPictureByPictureRequest searchPictureByPictureRequest) {
        return ResultUtils.success(pictureApplicationService.searchPictureByPicture(searchPictureByPictureRequest));
    }

    /**
     * 创建 AI 扩图任务
     */
    @PostMapping("/out_painting/create_task")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_EDIT)
    public BaseResponse<CreateOutPaintingTaskResponse> createPictureOutPaintingTask(
            @RequestBody CreatePictureOutPaintingTaskRequest createPictureOutPaintingTaskRequest,
            HttpServletRequest request) {
        return ResultUtils.success(pictureApplicationService.createPictureOutPaintingTask(createPictureOutPaintingTaskRequest, request));
    }

    /**
     * 查询 AI 扩图任务
     */
    @GetMapping("/out_painting/get_task")
    public BaseResponse<GetOutPaintingTaskResponse> getPictureOutPaintingTask(String taskId) {
        ThrowUtils.throwIf(StrUtil.isBlank(taskId), ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(aliYunAiApi.getOutPaintingTask(taskId));
    }


    /**
     * 按照颜色搜图
     *
     * @param searchPictureByColorRequest
     * @param request
     * @return
     */
    @PostMapping("/search/color")
    @ApiOperation("按照颜色搜图")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_VIEW)
    public BaseResponse<List<PictureVO>> searchPictureByColor(@RequestBody SearchPictureByColorRequest searchPictureByColorRequest, HttpServletRequest request) {
        return ResultUtils.success(pictureApplicationService.searchPictureByColor(searchPictureByColorRequest, request));
    }


    /**
     * 图片审核（仅管理员可用）
     *
     * @param pictureReviewRequest
     * @param request
     * @return
     */
    @PostMapping("/review")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation("图片审核（仅管理员可用）")
    public BaseResponse<Boolean> doPictureReview(@RequestBody PictureReviewRequest pictureReviewRequest,
                                                 HttpServletRequest request) {
        pictureApplicationService.doPictureReview(pictureReviewRequest, request);
        return ResultUtils.success(true);
    }

    /**
     * 获取标签分类（此处数据量不大，暂时写死即可）
     *
     * @return
     */

    @GetMapping("/tag_category")
    @ApiOperation("获取标签分类")
    public BaseResponse<PictureTagCategory> listPictureTagCategory() {
        PictureTagCategory pictureTagCategory = new PictureTagCategory();
        List<String> tagList = Arrays.asList("热门", "搞笑", "抽象", "生活", "高清", "超清", "4K", "治愈", "风景", "养眼", "艺术", "校园", "背景", "创意", "二次元", "原神", "nice");
        List<String> categoryList = Arrays.asList("模板", "电商", "表情包", "素材", "海报", "游戏", "写真", "壁纸", "默认");
        pictureTagCategory.setTagList(tagList);
        pictureTagCategory.setCategoryList(categoryList);
        return ResultUtils.success(pictureTagCategory);
    }
}